import {findCompanyDetailAPI, getRoleListAPI, deleteRoleAPI, addRoleAPI, findRoleDetailAPI, updateRoleAPI, assignPremAPI} from "@/api/settingsApi";

const actions = {
  /* 根据公司Id发送请求获取企业信息的action */
  async findCompanyDetail({commit}, id) {
    const {data: companyInfo} = await findCompanyDetailAPI(id)
    return companyInfo
  },
  /* 发送请求获取角色列表的action */
  async fetchRoleList({commit}, pageParams) {
    const {data} = await getRoleListAPI(pageParams)
    commit('updateRoleList', data)
  },
  /* 发送请求删除一个角色的action */
  async deleteRole({commit}, id) {
    const {message} = await deleteRoleAPI(id)
    return message
  },
  /* 发送请求添加一个角色的action */
  async addRole({commit}, role) {
    const {message} = await addRoleAPI(role)
    return message
  },
  /* 发送请求根据角色的id获取角色详情的action */
  async findRole({commit}, id) {
    const {data: role} = await findRoleDetailAPI(id)
    return role
  },
  /* 发送请求根据角色信息修改角色详情的action */
  async updateRole({commit}, role) {
    const {message} = await updateRoleAPI(role)
    return message
  },
  /* 发送请求给角色分配权限的action */
  async assignPrem({commit}, params) {
    const {message} = await assignPremAPI(params)
    return message
  }
}
const mutations = {
  /* 更新角色列表的mutation */
  updateRoleList(state, role) {
    state.role = role
  },
  addTotal(state, total) {
    state.role.total = total
  }
}
const state = () => {
  return {
    /* 角色信息*/
    role: {}
  }
}
const getters = {
  /* 角色列表 */
  roleList: state => state.role.rows || [],
  /* 总共多少条数据 */
  total: state => state.role.total || 0
}
export default {
  actions,
  mutations,
  state,
  getters,
  namespaced: true
}
